package com.hue.netty.day01.one;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;

/*
    客户端的处理器
 */
public class ClientHandler extends ChannelInboundHandlerAdapter {
    //通道准备就绪时会出发该方法
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("ctx:" + ctx);
        ctx.writeAndFlush(Unpooled.copiedBuffer("hello server...", CharsetUtil.UTF_8));
    }

    //有数据读取时触发
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        //将消息转换为ByteBuf
        ByteBuf buf = (ByteBuf) msg;
        System.out.println("客户端接收到服务器的消息：" + buf.toString(CharsetUtil.UTF_8));
    }
}
